import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
const routes = [
  {
    path: '/',
    name: 'Home',
    component: import ("../views/Home.vue")
  },
  {
    path: '/shop/:id',
    name: 'Shop',
    component: import ("../views/Shop.vue")
  },
  {
    path: '/confirmOrder/:id',
    name: 'confirmOrder',
    component: import ("../views/confirmOrder.vue")
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    beforeEnter: (to:Object,from:Object,next:Function)=>{
      const isLogin = localStorage.isLogin
      if(isLogin){
        //登录过
        console.log(to);
        next({name:'Home'})
      }else{
        console.log(to);
        next()
      }
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  }
]

const router = createRouter({
  // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
  history: createWebHashHistory(),
  routes, // `routes: routes` 的缩写
})
router.beforeEach((to, from, next) => {
  console.log(to, from);
  const isLogin = localStorage.isLogin
  if (isLogin || to.name === 'Login' || to.name === 'Register') {
    //说明已经登录了
    console.log("登录过");
    next()
  } else {
    //没有登录过
    console.log("没有登录过");
    next({ name: 'Login' })
  }


})
export default router